/**
 * Created by hqh on 2016/8/2.
 */

$(document).ready(function() {
    //选择司机 弹出页面
    $('#driverSelect').click(function(){
        //已被选择的线路ID
        var existDriversArr =[],
            length = $('#driverList tr').length;
        for(var i=0;i<length;i++){
            var item = $('#driverList tr').eq(i).find('td').eq(5).text();
            existDriversArr.push(item);
        }
        sessionStorage.setItem('selectedDriversID',JSON.stringify(existDriversArr));
        console.log('以下是已选择的司机ID:'+existDriversArr);
        console.log('已选司机存入sessionStorage：'+sessionStorage.getItem('selectedDriversID'));
        var index = layer.open({
            type: 2,
            shadeClose: false,
            area: ['1000px', '520px'],
            content: '/driversinfo/info',
            title: '司机信息',
            success: function (layero, index) {
                selectDrivers(layero,index);
            }
        });
    });

    //选择线路 弹出页面
    $('#circuitSelect').click(function(){
        //已被选择的线路ID
        var existLinesArr =[],
            length = $('#circuitList tr').length;
        for(var i=0;i<length;i++){
            var item = $('#circuitList tr').eq(i).find('td').eq(1).text();
            existLinesArr.push(item);
        }
        sessionStorage.setItem('selectedLinesID',JSON.stringify(existLinesArr));
        console.log('以下是已选择的线路ID:'+existLinesArr);
        console.log('已选线路存入sessionStorage：'+sessionStorage.getItem('selectedLinesID'));
        var index = layer.open({
            type: 2,
            shadeClose: false,
            area: ['1000px', '520px'],
            content: '/circuiteinfo/info',
            title: '线路信息',
            success: function (layero, index) {
                selectCircuits(layero,index);
            }
        });
    });
});


//选择司机
function selectDrivers(layero,index){
    var layerIframe = layero.find('iframe:first-child').contents();
    layerIframe.find('#confirm').click(function(){
        var checkArr = layerIframe.find('#driversInfoList input[type="checkbox"]'),
            checkedArrNum = layerIframe.find('#driversInfoList input[type="checkbox"]:checked').length;
        if(checkedArrNum == 0){
            bbx_alert_warning('您还没有选择任何司机!');
        }
        else{
            for(var i=0; i<checkArr.length; i++){
                if(checkArr[i].checked && !$(checkArr[i]).hasClass('disabled')){
                    var content = $(checkArr[i]).parent().siblings().clone();
                    var $wrapper = $('<div></div>').append(content);
                    var item = '<tr>'+$wrapper.html()+'<td><a class="del" href="javascript:void(0);" title="">删除</a></td></tr>';
                    $('#driverList').append(item);
                }
            }
            layer.close(index);
        }
    });

    layerIframe.find('#cancel').click(function(){
        layer.close(index);
    });
}
//删除司机
$(function(){
    $('#driverList').delegate('.del','click',function(){
        $(this).parents('tr').remove();
    });
});

//选择线路
function selectCircuits(layero,index){
    var layerIframe = layero.find('iframe:first-child').contents();
    layerIframe.find('#confirm').click(function(){
        var checkArr = layerIframe.find('#circuitInfoList input[type="checkbox"]'),
            checkedArrNum = layerIframe.find('#circuitInfoList input[type="checkbox"]:checked').length;
        if(checkedArrNum == 0){
            bbx_alert_warning('您还没有选择任何线路!');
        }
        else{
            for(var i=0; i<checkArr.length; i++){
                if(checkArr[i].checked && !$(checkArr[i]).hasClass('disabled')){
                    var content = $(checkArr[i]).parent().siblings().clone();
                    var $wrapper = $('<div></div>').append(content);
                    var item = '<tr>'+$wrapper.html()+'<td><a class="del" href="javascript:void(0);" title="">删除</a></td></tr>';
                    $('#circuitList').append(item);
                }
            }
            layer.close(index);
        }
    });

    layerIframe.find('#cancel').click(function(){
        layer.close(index);
    });
}

//删除线路
$(function(){
    $('#circuitList').delegate('.del','click',function(){
        $(this).parents('tr').remove();
    });
});
